﻿// Copyright (c) egmkang wang. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace Gateway
{
    using System;
    using System.Net;
    using System.Linq;
    using System.Net.Sockets;
    using Orleans;
    using Orleans.Concurrency;
    using Orleans.Runtime.Configuration;
    using XServer.GrainInterfaces;
    using XServer.SDK.Net;
    using XServer.SDK;

    class Program
    {
        static void StartServer()
        {
            var server = new Server(4);
            server.Listen<GatewayHandler>(8888);
        }

        static void Main(string[] args)
        {
            LoggerProvider.InitLogger(GatewaySettings.LogPrefix, GatewaySettings.LogLevel);

            StartServer();

            var gatewayObserver = new GatewayObserver();

            var config = ClientConfiguration.LocalhostSilo();
            GrainClient.Initialize(config);

            var allGateway = GrainClient.GrainFactory.GetGrain<IAllGatewayGrain>(0);
            var gateway = GrainClient.GrainFactory.GetGrain<IGatewayGrain>(String.Format("{0}:{1}", Platform.GetLocalIP(), GatewaySettings.Port));
            var observer = GrainClient.GrainFactory.CreateObjectReference<IGatewayObserver>(gatewayObserver).Result;
            gateway.RegisterObserver(gateway.GetPrimaryKeyString(), observer).Wait();
            allGateway.RegisterObserver(gateway.GetPrimaryKeyString(), observer).Wait();

            Console.WriteLine(GatewayHandler.GetPlayerID("openid_111", 222).Result);

            Console.ReadKey();
        }
    }
}
